package com.ruoyi.socket.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 币安WebSocket配置类
 * 管理多个币安WebSocket地址和切换逻辑
 */
@Component
@ConfigurationProperties(prefix = "binance.websocket")
public class BinanceWebSocketConfig {
    
    /**
     * 币安WebSocket地址列表
     */
    private List<String> urls = new ArrayList<>();
    
    /**
     * 当前使用的地址索引
     */
    private volatile int currentUrlIndex = 0;
    
    /**
     * 最大重试次数
     */
    private int maxRetries = 3;
    
    /**
     * 重连间隔（毫秒）
     */
    private long reconnectInterval = 5000;
    
    public BinanceWebSocketConfig() {
        // 默认地址列表
        urls.add("wss://stream.binance.com:9443");
        urls.add("wss://data-stream.binance.vision");
        urls.add("wss://stream1.binance.com:9443");
        urls.add("wss://stream2.binance.com:9443");
    }
    
    /**
     * 获取当前使用的WebSocket地址
     */
    public String getCurrentUrl() {
        if (urls.isEmpty()) {
            return "wss://stream.binance.com:9443";
        }
        return urls.get(currentUrlIndex);
    }
    
    /**
     * 切换到下一个地址
     */
    public String switchToNextUrl() {
        if (urls.size() <= 1) {
            return getCurrentUrl();
        }
        
        currentUrlIndex = (currentUrlIndex + 1) % urls.size();
        return getCurrentUrl();
    }
    
    /**
     * 重置到第一个地址
     */
    public void resetToFirstUrl() {
        currentUrlIndex = 0;
    }
    
    /**
     * 获取当前地址索引
     */
    public int getCurrentUrlIndex() {
        return currentUrlIndex;
    }
    
    /**
     * 获取地址总数
     */
    public int getUrlCount() {
        return urls.size();
    }
    
    /**
     * 构建完整的WebSocket URL
     */
    public String buildWebSocketUrl(String path) {
        return getCurrentUrl() + path;
    }
    
    // Getters and Setters
    public List<String> getUrls() {
        return urls;
    }
    
    public void setUrls(List<String> urls) {
        this.urls = urls;
    }
    
    public int getMaxRetries() {
        return maxRetries;
    }
    
    public void setMaxRetries(int maxRetries) {
        this.maxRetries = maxRetries;
    }
    
    public long getReconnectInterval() {
        return reconnectInterval;
    }
    
    public void setReconnectInterval(long reconnectInterval) {
        this.reconnectInterval = reconnectInterval;
    }
} 